package com.mysteel.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.mysteel.product.entity.ProductAttrValueEntity;
import com.mysteel.product.service.ProductAttrValueService;
import com.mysteel.product.vo.AttrResVo;
import com.mysteel.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import com.mysteel.product.entity.AttrEntity;
import com.mysteel.product.service.AttrService;
import com.mysteel.common.utils.PageUtils;
import com.mysteel.common.utils.R;



/**
 * 商品属性
 *
 * @author xc
 * @email xcremote@163.com
 * @date 2020-12-10 18:50:52
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService attrValueService;

    @PostMapping("/update/{spuId}")
    public R updateAttrBySpuId(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> attrList){
        attrValueService.updateSpuAttr(spuId,attrList);
        return R.ok();
    }

    @GetMapping("/base/listforspu/{spuId}")
    public R listAttrBySpuId(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> entityList = attrValueService.listAttrBySpu(spuId);
        return R.ok().put("data",entityList);
    }

    @GetMapping("/{attrType}/list/{catelogId}")
    // @RequiresPermissions("product:attr:list")
    public R baseListByCatelogId(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId,@PathVariable("attrType") String type){
        PageUtils page = attrService.queryBaseListPage(params,catelogId,type);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @Cacheable(value = "attr",key = "'attrInfo:' + #root.args[0]")
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrResVo resVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", resVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
